package com.nus.fellowmark.modules.assignment.service.impl;

import com.nus.fellowmark.dao.RubricDao;
import com.nus.fellowmark.entity.RubricEntity;
import com.nus.fellowmark.modules.assignment.dto.RubricDTO;
import com.nus.fellowmark.modules.assignment.service.QuestionService;
import com.nus.fellowmark.modules.assignment.service.RubricService;
import com.nus.fellowmark.modules.assignment.vo.QuestionVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nus.fellowmark.common.utils.PageUtils;
import com.nus.fellowmark.common.utils.Query;
import org.springframework.util.CollectionUtils;


@Service("rubricService")
public class RubricServiceImpl extends ServiceImpl<RubricDao, RubricEntity> implements RubricService {

    @Autowired
    RubricDao rubricDao;

    @Autowired
    QuestionService questionService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<RubricEntity> page = this.page(
                new Query<RubricEntity>().getPage(params),
                new QueryWrapper<RubricEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public Boolean createRubric(RubricDTO rubricDTO) {
        RubricEntity rubricEntity = new RubricEntity();
        BeanUtils.copyProperties(rubricDTO, rubricEntity);
        return this.save(rubricEntity);
    }

    @Override
    public List<RubricEntity> getRubricsByQuestionIds(List<Integer> questionIds) {
        QueryWrapper<RubricEntity> queryWrapper = new QueryWrapper<RubricEntity>().in("question_id", questionIds);
        return rubricDao.selectList(queryWrapper);
    }

    @Override
    public RubricEntity getRubricsByQuestionId(Integer id) {
        QueryWrapper<RubricEntity> queryWrapper = new QueryWrapper<RubricEntity>().eq("question_id", id);
        return rubricDao.selectOne(queryWrapper);
    }

    @Override
    public float getFullMarkOfAssignment(Integer assignmentId) {
        List<QuestionVO> questionVOS = questionService.listQuestions(assignmentId);
        if (CollectionUtils.isEmpty(questionVOS)) {
            return 0;
        }
        List<Integer> questionIds = questionVOS.stream().map(QuestionVO::getId).collect(Collectors.toList());
        List<RubricEntity> rubricEntities = rubricDao.selectList(new QueryWrapper<RubricEntity>().in("question_id", questionIds));
        if (CollectionUtils.isEmpty(rubricEntities)) {
            return 0;
        }
        int fullMark = rubricEntities.stream()
                .mapToInt(RubricEntity::getMaxMark) // Extract maxMark as int
                .sum(); // Sum the maxMark values
        return fullMark;
    }

}
